#!/bin/bash

echo "开始安装PHP扩展..."
php_ersion=$(php -r "echo substr(PHP_VERSION, 0, 3);")

#-------------------------------------------------------------------------------
# 安装 Xdebug
#-------------------------------------------------------------------------------
#if [ "$php_ersion" = "7.0" ]
#then
#  # 7.0版本
#  pecl install xdebug-2.7.2 \
#  && docker-php-ext-enable xdebug
#fi
#
#if [ "$php_ersion" = "7.1" ]
#then
#  # 7.1版本
#  pecl install xdebug-2.9.8 \
#  && docker-php-ext-enable xdebug
#fi
#
#if [ $(awk -v v1=${php_ersion} -v v2=7.2 'BEGIN{print(v1 >= v2) ? "1" : "0"}') -eq 1 ]
#then
#    # 7.2 以上版本
#    pecl install xdebug-3.1.4 \
#    && docker-php-ext-enable xdebug
#fi

#-------------------------------------------------------------------------------
# 安装 Redis
#-------------------------------------------------------------------------------
#pecl install redis-5.3.7 \
#&& docker-php-ext-enable redis

#-------------------------------------------------------------------------------
# 构建 GD 库
#-------------------------------------------------------------------------------
if [ $(awk -v v1=${php_ersion} -v v2=7.4 'BEGIN{print(v1 >= v2) ? "1" : "0"}') -eq 1 ]
then
    # 7.4 版本及以上构建 gd 库
    docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
else
    # 7.3 版本及以下构建 gd 库
    docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
fi

#-------------------------------------------------------------------------------
# 安装核心扩展
#-------------------------------------------------------------------------------
docker-php-ext-install -j$(nproc) gd mysqli pdo_mysql bcmath zip opcache sockets pcntl

#-------------------------------------------------------------------------------
# 安装 Composer
#-------------------------------------------------------------------------------
#if [ $(awk -v v1=${php_ersion} -v v2=7.2 'BEGIN{print(v1 >= v2) ? "1" : "0"}') -eq 1 ]
#then
#    # PHP 7.2 版本及以上
#    curl -o /usr/bin/composer https://getcomposer.org/download/latest-stable/composer.phar \
#    && chmod +x /usr/bin/composer \
#    && /usr/bin/composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
#else
    # PHP 7.1 版本以下
#    curl -o /usr/bin/composer https://getcomposer.org/download/latest-2.2.x/composer.phar \
#    && chmod +x /usr/bin/composer \
#    && /usr/bin/composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
#fi

#-------------------------------------------------------------------------------
# 安装 Swoole
# 注意，安装Swoole扩展时需不能安装 Xdebug 扩展
#-------------------------------------------------------------------------------
#if [ $(awk -v v1=${php_ersion} -v v2=8.0 'BEGIN{print(v1 >= v2) ? "1" : "0"}') -eq 1 ]
#then
#    pecl install swoole \
#    && docker-php-ext-enable swoole
#elif [ $(awk -v v1=${php_ersion} -v v2=7.2 'BEGIN{print(v1 >= v2) ? "1" : "0"}') -eq 1 ]
#then
#    pecl install swoole-4.8.12 \
#    && docker-php-ext-enable swoole
#else
#    pecl install swoole-2.2.0 \
#    && docker-php-ext-enable swoole
#fi

echo "PHP扩展安装结束..."